<%@ WebHandler Language="C#" Class="MD_LiquidationSales.ImageHandler" %>
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Configuration;
using System.Web.SessionState;
using WebLibs;

namespace MD_LiquidationSales
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ImageHandler : IHttpHandler, IRequiresSessionState
    {
        string strModule = string.Empty;
        string strFileName = string.Empty;

        Dictionary<string, string> _mapModule = new Dictionary<string, string>();

        public ImageHandler()
        {
            _mapModule.Add("liquid", Eworking.LiquidationSales.LiquidationControl.CurrentImageDir);
            _mapModule.Add("ERP.Item.Product", Globals.GetConfig("GEN_ImageDirectory") + "\\ERP\\Products");
        }

        public void ProcessRequest(HttpContext context)
        {
            if (context.Session["user"] == null)
            {
                context.Response.Write("Invalid session");
                return;
            }
            strModule = Globals.GetStrRequest("m", "");
            strModule = EncryptDecrypt.DecryptRijinDael(strModule.Replace(" ", "+"), Globals.GetConfig("SEC_FileSecureCode"));
            strFileName = Globals.GetStrRequest("f", "");
            strFileName = EncryptDecrypt.DecryptRijinDael(strFileName.Replace(" ", "+"), Globals.GetConfig("SEC_FileSecureCode"));
            if (string.IsNullOrEmpty(strModule) || string.IsNullOrEmpty(strFileName))
            {
                return;
            }

            ReturnImageFile(context, strModule, strFileName);
        }

        private void ReturnImageFile(HttpContext context, string strModule, string strFileName)
        {
            GenerateContentType(context, strFileName);
            try
            {
                context.Response.WriteFile(_mapModule[strModule] + "\\" + strFileName);
            }
            catch
            {
                context.Response.Write("Invalid image");
            }
        }

        private void GenerateContentType(HttpContext context, string strFileName)
        {
            string strExtension = string.Empty;
            strExtension = new System.IO.FileInfo(strFileName).Extension;
            switch (strExtension)
            {
                case "png":
                case ".png":
                    context.Response.ContentType = "image/png"; break;
                case "gif":
                case ".gif":
                    context.Response.ContentType = "image/gif"; break;
                case "jpg":
                case ".jpg":
                    context.Response.ContentType = "image/jpg"; break;
                case "jpeg":
                case ".jpeg":
                    context.Response.ContentType = "image/jpeg"; break;
                default: break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
